package com.project.decoration.controller.advantage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationAdvantage;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationAdvantageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 优势controller
 * @date 2025/02/12 11:50
 */
@Controller
@ResponseBody
@RequestMapping("advantage")
public class DecorationAdvantageController {

    @Autowired
    private DecorationAdvantageService decorationAdvantageService;

    /** 分页获取优势 */
    @PostMapping("getDecorationAdvantagePage")
    public Result getDecorationAdvantagePage(@RequestBody DecorationAdvantage decorationAdvantage) {
        Page<DecorationAdvantage> page = new Page<>(decorationAdvantage.getPageNumber(),decorationAdvantage.getPageSize());
        QueryWrapper<DecorationAdvantage> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(decorationAdvantage.getTitle()),DecorationAdvantage::getTitle,decorationAdvantage.getTitle())
                .like(StringUtils.isNotBlank(decorationAdvantage.getContent()),DecorationAdvantage::getContent,decorationAdvantage.getContent());
        Page<DecorationAdvantage> decorationAdvantagePage = decorationAdvantageService.page(page, queryWrapper);
        return Result.success(decorationAdvantagePage);
    }

    @GetMapping("getDecorationAdvantageList")
    public Result getDecorationAdvantageList() {
        List<DecorationAdvantage> advantageList = decorationAdvantageService.list();
        return Result.success(advantageList);
    }

    /** 根据id获取优势 */
    @GetMapping("getDecorationAdvantageById")
    public Result getDecorationAdvantageById(@RequestParam("id")String id) {
        DecorationAdvantage decorationAdvantage = decorationAdvantageService.getById(id);
        return Result.success(decorationAdvantage);
    }

    /** 保存优势 */
    @PostMapping("saveDecorationAdvantage")
    public Result saveDecorationAdvantage(@RequestBody DecorationAdvantage decorationAdvantage) {
        boolean save = decorationAdvantageService.save(decorationAdvantage);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑优势 */
    @PostMapping("editDecorationAdvantage")
    public Result editDecorationAdvantage(@RequestBody DecorationAdvantage decorationAdvantage) {
        boolean save = decorationAdvantageService.updateById(decorationAdvantage);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除优势 */
    @GetMapping("removeDecorationAdvantage")
    public Result removeDecorationAdvantage(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationAdvantageService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("优势id不能为空！");
        }
    }

}
